import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _uniqBy from "lodash/uniqBy";
import _map from "lodash/map";
import _isUndefined from "lodash/isUndefined";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _class, _class2;

/**
 * sendConfig - 发送配置
 * @date: 2018-9-7

 */
import React, { Component } from 'react';
import { connect } from 'dva';
import { routerRedux } from 'dva/router';
import { Bind } from 'lodash-decorators';
import { Header, Content } from 'components/Page';
import cacheComponent from 'components/CacheComponent';
import { Button as ButtonPermission } from 'components/Permission';
import formatterCollections from 'utils/intl/formatterCollections';
import intl from 'utils/intl';
import notification from 'utils/notification';
import { filterNullValueObject, isTenantRoleLevel } from 'utils/utils';
import { valueMapMeaning } from 'utils/renderer';
import ListTable from "./ListTable";
import Drawer from "./Drawer";
import SearchForm from "./SearchForm";
/**
 * 发送配置
 * @extends {Component} - React.Component
 * @reactProps {Object} [location={}] - 当前路由信息
 * @reactProps {Object} [match={}] - react-router match路由信息
 * @reactProps {!Object} approveAuth - 数据源
 * @reactProps {!Object} fetchTableListLoading - 列表数据加载是否完成
 * @reactProps {!Object} sendMessageLoading - 消息发送是否完成
 * @reactProps {Object} form - 表单对象
 * @reactProps {Function} [dispatch=function(e) {return e;}] - redux dispatch方法
 * @return React.element
 */

var List = (_dec = connect(function (_ref) {
  var sendConfig = _ref.sendConfig,
      loading = _ref.loading;
  return {
    sendConfig: sendConfig,
    tenantRoleLevel: isTenantRoleLevel(),
    fetchTableListLoading: loading.effects['sendConfig/queryTableList'],
    sendMessageLoading: loading.effects['sendConfig/sendMessage'],
    getParamsLoading: loading.effects['sendConfig/getParams']
  };
}), _dec2 = formatterCollections({
  code: ['cmsg.sendConfig', 'entity.tenant', 'entity.lang', 'cmsg.common']
}), _dec3 = cacheComponent({
  cacheKey: '/cmsg/sendConfig'
}), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec13 = Bind(), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = /*#__PURE__*/function (_Component) {
  _inherits(List, _Component);

  var _super = _createSuper(List);

  function List() {
    var _this;

    _classCallCheck(this, List);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.form = void 0;
    _this.state = {
      visible: false,
      // 发送模态框是否可见
      tableRecord: {},
      // 表格中数据
      enableService: [],
      // 启用服务数组
      isCopy: false
    };
    return _this;
  }

  _createClass(List, [{
    key: "componentDidMount",
    value:
    /**
     * render()调用后获取数据
     */
    function componentDidMount() {
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          _this$props$sendConfi = _this$props.sendConfig.pagination,
          pagination = _this$props$sendConfi === void 0 ? {} : _this$props$sendConfi,
          _this$props$location$ = _this$props.location.state;
      _this$props$location$ = _this$props$location$ === void 0 ? {} : _this$props$location$;
      var _back = _this$props$location$._back; // 校验是否从详情页返回

      var page = _isUndefined(_back) ? {} : pagination;
      this.handleSearch(page);
      dispatch({
        type: 'sendConfig/queryMessageType'
      });
    }
    /**
     * 查询
     * @param {object} fields - 查询参数
     */

  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var fields = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      var fieldValues = _isUndefined(this.form) ? {} : filterNullValueObject(this.form.getFieldsValue());
      dispatch({
        type: 'sendConfig/queryTableList',
        payload: _objectSpread({
          page: _isEmpty(fields) ? {} : fields
        }, fieldValues)
      });
    }
    /**
     * 新增，跳转到明细页面
     */

  }, {
    key: "handleAddApprove",
    value: function handleAddApprove() {
      var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      dispatch(routerRedux.push({
        pathname: "/cmsg/send-config/create",
        payload: payload
      }));
    }
    /**
     * 数据列表，行删除
     * @param {obejct} record - 操作对象
     */

  }, {
    key: "handleDeleteContent",
    value: function handleDeleteContent(record) {
      var _this2 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          pagination = _this$props2.sendConfig.pagination;
      dispatch({
        type: 'sendConfig/deleteHeader',
        payload: record
      }).then(function (res) {
        if (res) {
          notification.success();

          _this2.handleSearch(pagination);
        }
      });
    }
    /**
     * 数据列表，行编辑
     *@param {obejct} record - 操作对象
     */

  }, {
    key: "handleEditContent",
    value: function handleEditContent(record) {
      var _this3 = this;

      var dispatch = this.props.dispatch;
      this.setState({
        isCopy: false
      }, function () {
        var isCopy = _this3.state.isCopy;

        var payload = _objectSpread(_objectSpread({}, record), {}, {
          isCopy: isCopy
        });

        dispatch(routerRedux.push({
          pathname: "/cmsg/send-config/detail/".concat(record.tempServerId),
          payload: payload
        }));
      });
    }
  }, {
    key: "handleCopy",
    value: function handleCopy(record) {
      var _this4 = this;

      this.setState({
        isCopy: true
      }, function () {
        var isCopy = _this4.state.isCopy;

        var payload = _objectSpread(_objectSpread({}, record), {}, {
          isCopy: isCopy
        });

        _this4.handleAddApprove(payload);
      });
    }
    /**
     * 打开发送模态框
     *
     * @memberof List
     */

  }, {
    key: "handleOpenSendModal",
    value: function handleOpenSendModal(record) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'sendConfig/fetchLangType',
        payload: {
          tenantId: record.tenantId,
          messageCode: record.messageCode
        }
      });
      var anotherServerList = [];
      var tempServerList = [];

      if (record.typeCode) {
        tempServerList.push({
          typeMeaning: record.typeMeaning,
          typeCode: record.typeCode
        });
      }

      if (!_isEmpty(record.serverList)) {
        anotherServerList = _uniqBy(record.serverList.map(function (item) {
          return {
            typeMeaning: item.typeMeaning,
            typeCode: item.typeCode
          };
        }), 'typeCode');
      }

      this.setState({
        visible: true,
        tableRecord: _objectSpread({}, record),
        enableService: [].concat(tempServerList, _toConsumableArray(anotherServerList))
      });
      var langParams = {
        lang: 'zh_CN',
        tenantId: record.tenantId,
        messageCode: record.messageCode
      };
      this.getParams(langParams);
    }
    /**
     * 关闭发送模态框
     *
     * @memberof List
     */

  }, {
    key: "handleCloseSendModal",
    value: function handleCloseSendModal() {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'sendConfig/updateState',
        payload: {
          paramsName: []
        }
      });
      this.setState({
        visible: false,
        tableRecord: {},
        enableService: []
      });
    }
    /**
     * 发送保存
     *
     * @param {*} values
     * @memberof List
     */

  }, {
    key: "handleSendOk",
    value: function handleSendOk(values) {
      var _this$props3 = this.props,
          dispatch = _this$props3.dispatch,
          _this$props3$sendConf = _this$props3.sendConfig.messageType,
          messageType = _this$props3$sendConf === void 0 ? [] : _this$props3$sendConf;
      dispatch({
        type: 'sendConfig/sendMessage',
        payload: values
      }).then(function (res) {
        if (res) {
          var newRes = [];
          newRes = _map(res, function (value, key) {
            return {
              key: key,
              value: value
            };
          });
          newRes.map(function (item) {
            return item.value === 1 ? notification.success({
              message: "".concat(valueMapMeaning(messageType, item.key)).concat(intl.get('cpaas.common.status.success').d('成功'))
            }) : notification.error({
              message: "".concat(valueMapMeaning(messageType, item.key)).concat(intl.get('cpaas.common.status.error').d('失败'))
            });
          });
        }
      });
    }
    /**
     * 设置Form
     * @param {object} ref - FilterForm组件引用
     */

  }, {
    key: "handleBindRef",
    value: function handleBindRef() {
      var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.form = (ref.props || {}).form;
    }
    /**
     * 改变语言，获取参数
     */

  }, {
    key: "getParams",
    value: function getParams(values) {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'sendConfig/getParams',
        payload: values
      });
    }
    /**
     * render
     * @returns React.element
     */

  }, {
    key: "render",
    value: function render() {
      var _this$props4 = this.props,
          fetchTableListLoading = _this$props4.fetchTableListLoading,
          sendMessageLoading = _this$props4.sendMessageLoading,
          getParamsLoading = _this$props4.getParamsLoading,
          _this$props4$sendConf = _this$props4.sendConfig,
          _this$props4$sendConf2 = _this$props4$sendConf.list,
          list = _this$props4$sendConf2 === void 0 ? [] : _this$props4$sendConf2,
          _this$props4$sendConf3 = _this$props4$sendConf.scopeType,
          scopeType = _this$props4$sendConf3 === void 0 ? [] : _this$props4$sendConf3,
          pagination = _this$props4$sendConf.pagination,
          _this$props4$sendConf4 = _this$props4$sendConf.langType,
          langType = _this$props4$sendConf4 === void 0 ? [] : _this$props4$sendConf4,
          _this$props4$sendConf5 = _this$props4$sendConf.paramsName,
          paramsName = _this$props4$sendConf5 === void 0 ? [] : _this$props4$sendConf5,
          tenantRoleLevel = _this$props4.tenantRoleLevel,
          path = _this$props4.match.path;
      var _this$state = this.state,
          visible = _this$state.visible,
          tableRecord = _this$state.tableRecord,
          enableService = _this$state.enableService;
      var filterProps = {
        tenantRoleLevel: tenantRoleLevel,
        onSearch: this.handleSearch,
        onRef: this.handleBindRef
      };
      var listProps = {
        path: path,
        scopeType: scopeType,
        pagination: pagination,
        tenantRoleLevel: tenantRoleLevel,
        loading: fetchTableListLoading,
        dataSource: list,
        onEdit: this.handleEditContent,
        onDelete: this.handleDeleteContent,
        onOpenSendModal: this.handleOpenSendModal,
        onChange: this.handleSearch,
        onCopy: this.handleCopy
      };
      var drawerProps = {
        path: path,
        visible: visible,
        tableRecord: tableRecord,
        langType: langType,
        enableService: enableService,
        paramsName: paramsName,
        tenantRoleLevel: tenantRoleLevel,
        saving: sendMessageLoading,
        getParamsLoading: getParamsLoading,
        anchor: 'right',
        onCancel: this.handleCloseSendModal,
        onSendOk: this.handleSendOk,
        onGetParams: this.getParams
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cmsg.sendConfig.view.message.title.sendConfig').d('消息发送配置')
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.create"),
          type: 'button',
          meaning: '消息发送配置-新建'
        }],
        icon: "plus",
        type: "primary",
        onClick: this.handleAddApprove
      }, intl.get('cpaas.common.button.create').d('新建'))), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(SearchForm, filterProps)), /*#__PURE__*/React.createElement(ListTable, listProps)), /*#__PURE__*/React.createElement(Drawer, drawerProps));
    }
  }]);

  return List;
}(Component), (_applyDecoratedDescriptor(_class2.prototype, "handleSearch", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddApprove", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddApprove"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDeleteContent", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDeleteContent"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEditContent", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEditContent"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCopy", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCopy"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleOpenSendModal", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOpenSendModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCloseSendModal", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCloseSendModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSendOk", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSendOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleBindRef", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleBindRef"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "getParams", [_dec13], Object.getOwnPropertyDescriptor(_class2.prototype, "getParams"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { List as default };